#include "registerdialog.h"
#include "ui_registerdialog.h"
#include "data_bean.h"
#include "utils.h"
#include "global_var.h"

#include <QMessageBox>

RegisterDialog::RegisterDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RegisterDialog)
{
    ui->setupUi(this);

    init();
}

RegisterDialog::~RegisterDialog()
{
    delete ui;
}

void RegisterDialog::on_push_button_clicked() {
    if(is_user_id_exist(ui->input_uid->text())) {
        QMessageBox::information(this, tr("Error"), tr("该id已存在"), QMessageBox::Yes);
        return;
    }

    USER_ROLE role;
    if(ui->cbx_role->currentText() == "教师") {
        role = TEACHER;
    }
    else if(ui->cbx_role->currentText() == "学生") {
        role = STUDENT;
    }
    else {
        role = STUDENT;
    }

    User user = User(ui->input_uid->text(),
                     ui->input_pwd->text(),
                     role,
                     ui->input_email->text(),
                     ui->input_tel->text());

    users.push_back(user);
}

void RegisterDialog::init() {
    init_combo_box();
}

void RegisterDialog::init_combo_box() {
    ui->cbx_role->addItem("教师");
    ui->cbx_role->addItem("学生");
}

void RegisterDialog::on_buttonBox_accepted()
{
    if(is_user_id_exist(ui->input_uid->text())) {
        QMessageBox::information(this, tr("Error"), tr("该id已存在"), QMessageBox::Yes);
        return;
    }

    USER_ROLE role;
    if(ui->cbx_role->currentText() == "教师") {
        role = TEACHER;
    }
    else if(ui->cbx_role->currentText() == "学生") {
        role = STUDENT;
    }
    else {
        role = STUDENT;
    }

    User user = User(ui->input_uid->text(),
                     ui->input_pwd->text(),
                     role,
                     ui->input_email->text(),
                     ui->input_tel->text());

    users.push_back(user);

    QMessageBox::information(this, tr("Success"), tr("注册成功"), QMessageBox::Yes);

    write_users();

    this->close();
}
